//
//  FileListViewController.m
//  jinnengExam
//
//  Created by mac on 2020/8/10.
//  Copyright © 2020 jznrj. All rights reserved.
//

#import "FileListViewController.h"
#import "MBProgressHUD.h"
#import "AFHTTPRequestOperation.h"
#import "FileDetailViewController.h"
@interface FileListViewController ()

@end

@implementation FileListViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"资料详情";
    UINavigationBar * bar = self.navigationController.navigationBar;
    UIImage *bgImage = [ColorUtil navigationBarColor:[ColorUtil titleColor] BarSize:bar.frame alphe:1.0];
    [bar setBackgroundImage:bgImage forBarMetrics:UIBarMetricsDefault];
    [bar setTintColor:[UIColor whiteColor]];
    [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]}forState:UIControlStateNormal];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,nil]];
    [self initView];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
-(void) initView{
    [self.view addSubview:self.jznWebView];
    [self.jznWebView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view);
    }];
    [self.jznWebView loadLocalFile:@"/ios_jinneng/TrainingClassDetail" type:@"html"];
    
}


- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
    
    NSString *detailJsString = [NSString stringWithFormat:@"getNewsDetail(%@)",[self fileListString]];
    [self.jznWebView evaluateJavaScript: detailJsString completionHandler:^(id _Nullable data, NSError * _Nullable error) {
        
    }];
}



- (BOOL)navigationShouldPopOnBackButton { //在这个方法里写返回按钮的事件处理
    if ([self.jznWebView canGoBack]) {
        [self.jznWebView goBack];
        return NO;
    }
    return YES; //返回NO 不会执行
}

- (void)downloadFileWithUrl:(NSString*)fileUrl saveFilePath:(NSString *)filePath fileName:(NSString *)fileName andBlock:(void (^)(BOOL isSuccess, NSError *error))block
{
    NSString *path = [NSString stringWithFormat:@"%@%@", filePath, fileName];
    BOOL isDir = NO;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL existed = [fileManager fileExistsAtPath:filePath isDirectory:&isDir];
    if ( !(isDir == YES && existed == YES) )
    {
        [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    fileUrl = [fileUrl stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
    //创建 Request
    NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:fileUrl]];
    //下载进行中的事件
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
    [operation setOutputStream:[NSOutputStream outputStreamToFileAtPath:path append:NO]];
    [operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
        NSLog(@"is download：%f", (float)totalBytesRead / totalBytesExpectedToRead);
    }];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        block(YES, nil);
        NSLog(@"下载成功");
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        block(NO, error);
        NSLog(@"下载失败");
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    }];
    [operation start];
}

@end
